package controller;

import java.util.ArrayList;
import java.util.List;

import model.components.Component;

public class FakeNetwork {
	List<Component> connectedComponent = new ArrayList<Component>();

	public FakeNetwork() {
	};

	public boolean connectComponent(Component c) {
		return connectedComponent.add(c);
	}

	public boolean broadcastItem(PoolItem poolItem) {
		for (Component c : connectedComponent) {
			if (!c.insertToPool("event", poolItem))
				return false;
		}
		return true;
	}

	public boolean SendItemToComponentById(PoolItem poolItem, int componentId) {
		for (Component c : connectedComponent) {
			if (c.getComponentId() == componentId) {
				return c.insertToPool("event", poolItem);
			}
		}
		return false;
	}

	public Object SendItemToComponentByLabel(PoolItem poolItem, String componentLabel) {
		for (Component c : connectedComponent) {
			if (c.getComponentLabel().compareTo(componentLabel) == 0) {
				return c.insertToPool("event", poolItem);
			}
		}
		return false;
	}

	public Component getComponentById(int pComponentId) {
		for (int i = 0; i < connectedComponent.size(); i++) {
			if (connectedComponent.get(i).getComponentId() == pComponentId)
				return connectedComponent.get(i);
		}
		return null;
	}

	public Component getComponentByLabel(String pComponentLabel) {
		for (int i = 0; i < connectedComponent.size(); i++) {
			if (connectedComponent.get(i).getComponentLabel().equals(pComponentLabel))
				return connectedComponent.get(i);
		}
		return null;
	}

	public Component getLastAddedComponent() {
		if(connectedComponent.size() > 0)
			return connectedComponent.get(connectedComponent.size() - 1);
		else
			return null;
	}

}
